package com.lgl.scs.system.timer;

import com.lgl.scs.userIndex.service.ServiceOrderService;
import com.lgl.scs.util.service.UtilService;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单类定时器
 */
@Slf4j
@Component
public class ServiceOrderTimer {
    @Resource(name="utilService")
    private UtilService utilService;
    @Resource(name="serviceOrderService")
    private ServiceOrderService serviceOrderService;

    /**
     * 订单过期
     * 每天0点10分执行
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void orderExpire(){
        PageData expirePd = new PageData(2);
        expirePd.put("soEndServiceDate", DateUtil.getNowDate("-"));
        expirePd.put("soStatus","0");
        List<PageData> expireOrderList = serviceOrderService.listServiceOrder(expirePd);
        if (expireOrderList == null || expireOrderList.isEmpty()){
            log.info("\n-------------本次没有要执行过期的订单");
            return;
        }
        String expireOrderId = getOrderId(expireOrderList);
        log.info("\n---------开始执行[订单过期]定时任务,过期的单号有：" + expireOrderId);
        String expireSql = "update cor_service_order set so_status=6,issue_finish_date=now() where so_status=0 and so_bill_number in (" + expireOrderId + ")";
        int expireNum = utilService.updateDataBySql(expireSql);
        log.info("\n---------结束执行[订单过期]定时任务,成功过期" + expireNum + "了条订单.");
    }

    /**
     * 对于抢单人已完成，而发布人未执行完成操作的，且日期超过7天的，对订单执行自动完成操作
     * 每天0点20分执行
     */
    @Scheduled(cron = "0 20 0 * * ?")
    public void autoCompleteOrder(){
        PageData autoCompletePd = new PageData(2);
        autoCompletePd.put("notStrRobFinishDate", "date_sub(now(),interval 5 day)");
        autoCompletePd.put("soStatus","4");
        List<PageData> autoCompleteList = serviceOrderService.listServiceOrder(autoCompletePd);
        if (autoCompleteList == null || autoCompleteList.isEmpty()){
            log.info("\n+++本次没有要自动完成操作的订单+++");
            return;
        }
        String autoCompleteId = getOrderId(autoCompleteList);
        log.info("\n++开始执行[待确认订单自动完成]定时任务,过期的单号有：" + autoCompleteId);
        String expireSql = "update cor_service_order set so_status=2,issue_finish_date=now() where so_status=4 and so_bill_number in (" + autoCompleteId + ")";
        int expireNum = utilService.updateDataBySql(expireSql);
        log.info("\n++结束执行[待确认订单自动完成]定时任务,成功过期" + expireNum + "了条订单.");
    }

    /**
     * 获取订单ID
     * @param pageDataList  订单集合
     * @return 获取订单ID字符串
     */
    private String getOrderId(List<PageData> pageDataList){
        StringBuilder orderId = new StringBuilder();
        for (PageData pageData : pageDataList) {
            orderId.append("'").append(pageData.get("billNumber")).append("',");
        }
        orderId.deleteCharAt(orderId.length() - 1);
        return orderId.toString();
    }
}